//
//  ViewController.m
//  WebSocketDemo
//
//  Created by 孙俊 on 17/2/16.
//  Copyright © 2017年 newbike. All rights reserved.
//

#import "ViewController.h"
#import "SocketRocketUtility.h"
#import "LogTableViewCell.h"

@interface ViewController () <SocketRocketUtilityDelegate>
@property (weak, nonatomic) IBOutlet UITableView *logTableView;
@property (weak, nonatomic) IBOutlet UIButton *connectBtn;
@property (weak, nonatomic) IBOutlet UIButton *disConnectBtn;
@property (weak, nonatomic) IBOutlet UIButton *cleanLogBtn;
@property (weak, nonatomic) IBOutlet UILabel *scoketNum;
@property (weak, nonatomic) IBOutlet UILabel *historySocketNum;
@property (weak, nonatomic) IBOutlet UILabel *failSocketNum;
@property (nonatomic,strong) NSMutableArray *dataArr;
@property (nonatomic,assign) NSInteger reciveMessageCount;
@property (nonatomic,assign) NSInteger historyMessageCount;
@property (nonatomic,assign) NSInteger failMessageCount;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.reciveMessageCount = 0;
    self.historyMessageCount = 0;
    self.failMessageCount = 0;
    [SocketRocketUtility instance].delegate = self;
    
    [self.logTableView registerNib:[UINib nibWithNibName:LogTableCell bundle:nil] forCellReuseIdentifier:LogTableCellID];
    self.logTableView.estimatedRowHeight = 44;//推测高度
}

- (IBAction)connectSocket:(id)sender {
    [[SocketRocketUtility instance] SRWebSocketOpenWithURLString:@"ws://172.16.100.171:8002/connection/websocket"
                                                           token:@"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxOTAzIn0.1ODZESrT0kIYvftKFKHdUZsN9lRibO5-qoJL_aoALwc"
                                                        channels:@[@"tttttt",@"sp1:tttttt"]];
}

- (IBAction)disConnectScoket:(id)sender {
    [[SocketRocketUtility instance] SRWebSocketClose];
}

- (IBAction)cleanLog:(id)sender {
    [self.dataArr removeAllObjects];
    [self.dataArr addObject:@"日志:"];
    self.reciveMessageCount = 0;
    self.scoketNum.text = @"0";
    self.historyMessageCount = 0;
    self.historySocketNum.text = @"0";
    self.failMessageCount = 0;
    self.failSocketNum.text = @"0";
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.logTableView reloadData];
    });
}

#pragma mark SocketRocketUtilityDelegateMethods

- (void) socketTokenReciveScucess:(NSString *)scucessMessage {
    [self.dataArr addObject:scucessMessage];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.logTableView reloadData];
        [self.view layoutIfNeeded];
        [self scrollViewToBottom:YES];
    });
}

- (void) socketConnectChannelScucess:(NSString *)scucessMessage {
    [self.dataArr addObject:scucessMessage];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.logTableView reloadData];
        [self.view layoutIfNeeded];
        [self scrollViewToBottom:YES];
        
    });
}

- (void) scoketTipsMessage:(NSString *)tips {
    [self.dataArr addObject:tips];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.logTableView reloadData];
        [self.view layoutIfNeeded];
        [self scrollViewToBottom:YES];
    });
}

- (void) scoketPostMessage:(NSString *)postMessage {
    [self.dataArr addObject:postMessage];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.logTableView reloadData];
        [self.view layoutIfNeeded];
        [self scrollViewToBottom:YES];
    });
}

- (void) socketReciveMessage:(NSString *)recevedMessage {
    self.reciveMessageCount ++;
    self.scoketNum.text = [NSString stringWithFormat:@"%ld",self.reciveMessageCount];
    [self.dataArr addObject:recevedMessage];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.logTableView reloadData];
        [self.view layoutIfNeeded];
        [self scrollViewToBottom:YES];
    });
}

- (void) scoketReciveHistoryMessage:(NSString *)recevedMessage {
    self.historyMessageCount ++;
    self.historySocketNum.text = [NSString stringWithFormat:@"%ld",self.historyMessageCount];
    [self.dataArr addObject:recevedMessage];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.logTableView reloadData];
        [self.view layoutIfNeeded];
        [self scrollViewToBottom:YES];
    });
}

- (void) socketAnalysisMessageFail:(NSString *)failMessage {
    self.failMessageCount ++;
    self.failSocketNum.text = [NSString stringWithFormat:@"%ld",self.failMessageCount];
    [self.dataArr addObject:failMessage];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.logTableView reloadData];
        [self.view layoutIfNeeded];
        [self scrollViewToBottom:YES];
    });
}

#pragma mark tableViewDelegateMethods
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LogTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LogTableCellID];
    if (!cell) {
        cell = [[LogTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:LogTableCellID];
    }
    cell.logLabel.text = self.dataArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc]initWithCapacity:0];
        [_dataArr addObject:@"日志:"];
    }
    return _dataArr;
}

- (void)scrollViewToBottom:(BOOL)animated {
    
    if (self.logTableView.contentSize.height > self.logTableView.frame.size.height){
        CGPoint offset = CGPointMake(0, self.logTableView.contentSize.height -self.logTableView.frame.size.height);
        [self.logTableView setContentOffset:offset animated:animated];
    }
    
}



@end
